//
//  NewCollectionViewCell.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NewCollectionViewCell.h"
#import "NewOneTableViewCell.h"
#import "NewTwoTableViewCell.h"
#import "NewThreeTableViewCell.h"
#import "NewsModel.h"
#import "NewsArrModel.h"
#import "SDCycleScrollView.h"
#import "NewsModel.h"
#import "NewsArrModel.h"
@interface NewCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource, SDCycleScrollViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SDCycleScrollView *cycSV;

@end

@implementation NewCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubview];
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    self.tableView.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, self.contentView.frame.size.height - 170);
    
}

- (void)setArr:(NSMutableArray *)arr
{
    
    if (_arr != arr) {
        _arr = arr;
        
       [self createHeadView];
        
    }
   
     [self.tableView reloadData];
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    
}

- (void)createSubview
{
    self.backgroundColor = [UIColor whiteColor];
    [self createTableView];
}
- (void)createHeadView
{
    if ([[self.arr objectAtIndex:0] ads].count) {
        NSMutableArray *imagesURLStringArr = [NSMutableArray array];
        NSMutableArray *titleArr = [NSMutableArray array];
        for (NSInteger i = 0; i < [[self.arr objectAtIndex:0] ads].count; i++) {
            NSString *imagesURLString = [[[[self.arr objectAtIndex:0] ads] objectAtIndex:i] objectForKey:@"imgsrc"];
            NSString *title = [[[[self.arr objectAtIndex:0] ads] objectAtIndex:i] objectForKey:@"title"];
            [imagesURLStringArr addObject:imagesURLString];
            [titleArr addObject:title];
        }
        
        self.cycSV = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(10 * WIDTH, 300 * HEIGHT, [UIScreen mainScreen].bounds.size.width - 20 * WIDTH, 200 * HEIGHT) delegate:self placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        self.cycSV.imageURLStringsGroup = imagesURLStringArr;
        self.cycSV.titlesGroup = titleArr;
        self.cycSV.showPageControl = NO;
        self.tableView.tableHeaderView = self.cycSV;

    } else {
    
        self.tableView.tableHeaderView = nil;
    
    }
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.contentView addSubview:self.tableView];
    [self pullToRefresh];
    [self upToLoadMoreData];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (1 == [[[self.arr objectAtIndex:indexPath.row] imgType] integerValue]) {
        return 250 * HEIGHT;
    }
    
    if (nil != [[self.arr objectAtIndex:indexPath.row] imgextra]) {
        return 130 * HEIGHT;
    }
    return 100;
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel *model = [self.arr objectAtIndex:indexPath.row];
    if (nil != model.imgextra) {
            static NSString *cellTwo = @"cellTO";
            NewTwoTableViewCell *cellTO = [tableView dequeueReusableCellWithIdentifier:cellTwo];
            if (nil == cellTO) {
                cellTO = [[NewTwoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellTwo];
        
            }
        cellTO.labelTitle.text = model.title;
        [cellTO.imageL sd_setImageWithURLStr:model.imgsrc placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        [cellTO.imageC sd_setImageWithURLStr:[[model.imgextra objectAtIndex:0] objectForKey:@"imgsrc"] placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        [cellTO.imageR sd_setImageWithURLStr:[[model.imgextra objectAtIndex:1] objectForKey:@"imgsrc"] placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
            return cellTO;
    }
    if (1 == [model.imgType integerValue]) {
        static NSString *cellThree = @"cellT";
        NewThreeTableViewCell *cellT = [tableView dequeueReusableCellWithIdentifier:cellThree];
        if (nil == cellT) {
            cellT = [[NewThreeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellThree];
        }
        cellT.labelTitle.text = model.title;
        cellT.labelSubject.text = model.digest;
        [cellT.image sd_setImageWithURLStr:model.imgsrc placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        return cellT;
    } else {
        static NSString *cellOne = @"cellO";
        NewOneTableViewCell *cellO = [tableView dequeueReusableCellWithIdentifier:cellOne];
        if (nil == cellO) {
            cellO = [[NewOneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellOne];
        }
        cellO.labelTitle.text = model.title;
        cellO.labelSubject.text = model.digest;
        [cellO.image sd_setImageWithURLStr:model.imgsrc placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        return cellO;
    }
}
// 下拉刷新
- (void)pullToRefresh
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadNewData
{
    [self.delegate pullToRefresh];
}


// 上拉加载
- (void)upToLoadMoreData
{
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(LaodMoewDataAction)];

    [self.tableView.mj_footer beginRefreshing];

}

- (void)LaodMoewDataAction
{
    [self.delegate upToLadModeBypage:self.page];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 点击时颜色选中, 松开颜色放去
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.delegate returnDetailByIndex:indexPath.row page:self.page];
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    [self.delegate scrollViewToDetailByUrl:[[[[self.arr objectAtIndex:0] ads] objectAtIndex:index] objectForKey:@"url"]];
}



@end
